// pages/todayAppointments/todayAppointments.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: getApp().globalData.url,
    username: '',
    //显示数据
    patient: [],
    scrollHeight: 0,
    currentId: -1,
    patient1: [{
      logoLink: '/images/defaultImage.png',
      patientName: '小红',
      orderDate: '2020-11-31',
      orderTime: '上午',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小明',
      orderDate: '2020-12-11',
      orderTime: '下午',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小白',
      orderDate: '2020-11-31',
      orderTime: '上午',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小刘',
      orderDate: '2020-12-09',
      orderTime: '上午',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小兰',
      orderDate: '2020-12-05',
      orderTime: '下午',
      patientTel: "18727508188"
    },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh();
    this.setData({
      username: getApp().globalData.username,
    })
    console.log(23)
    console.log(this.data.patient)
    this.getPatient()
    console.log(this.data.patient)
    
    try {
      var ap = wx.getStorageSync('Patient')
      this.setData({
        patient: ap
      })
    } catch(e) { }

    var that = this;
    //动态scrollview
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scrollHeight: (windowHeight - 0 - 60) * 750 / windowWidth
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.getPatient()

    this.setData({
      username: getApp().globalData.username,
    })
    try {
      var ap = wx.getStorageSync('Patient')
      this.setData({
        patient: ap
      })
    } catch(e) { }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        // console.info(res.windowHeight);
        let height = res.windowHeight;
        wx.createSelectorQuery().selectAll('.list_top').boundingClientRect(function (rects) {
          rects.forEach(function (rect) {
            that.setData({
              scrollHeight: height - rect.bottom
            });
          })
        }).exec();
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getPatient()
  },

  /**
   * 跳转到近期预约
   */
  navigateToRecentAppointment: function(){
    wx.switchTab({
      url: '../appointments/appointments',
    })
  },

  /**
   * 获取病人信息
   */
  getPatient: function () {
    var that = this
    var id = this.data.username
    wx.request({
      url: this.data.url + 'getPatientTodayServlet',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        doctorID: id
      },
      success: function (res) {
        console.log(1)
        console.log(res.data)
        if (res.data != false && res.data != "False" && res.data != "空") {
          try {
            wx.setStorageSync("Patient", res.data)
          } catch (e) { }
          that.setData({
            patient: res.data
          })
          console.log(that.data.patient)
        }
        else if(res.data == "空" || res.data == "False") {
          that.setData({
            patient: null
          })
        }
        else {
          wx.showToast({
            title: '获取失败，请从顶部下拉刷新',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '获取失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 滑动事件
   */
  backToStart: function(e) {
    if (this.data.currentId != e.currentTarget.dataset.idx && this.data.currentId != -1) {
      this.setData({
        scl: 0
      })
    }
    this.setData({
      currentId: e.currentTarget.dataset.idx
    })
  },

  /**
   * 发送消息
   */
  sendMessage: function(e) {
    var id = e.currentTarget.dataset.idx
    var tel = this.data.patient[id].patientTel
    var orderDate = this.data.patient[id].orderDate
    var orderTime = this.data.patient[id].orderTime

    wx.request({
      url: this.data.url + 'sendMessageServlet',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        doctorTel: getApp().globalData.username,
        patientTel: tel,
        bookDate: orderDate,
        bookTime: orderTime,
      },
      success(res) {
        wx.showToast({
          title: '已成功发送消息提醒该患者就诊',
          icon: 'success',
        })
      },
      fail(res) {
        wx.showToast({
          title: '信息发送失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 给病人开病历
   */
  medicalRecord: function(e){
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    var patientTel = JSON.stringify(that.data.patient[index].patientTel)
    var patientOrderDate = JSON.stringify(that.data.patient[index].orderDate)
    var patientOrderTime = JSON.stringify(that.data.patient[index].orderTime)
    wx.navigateTo({
      url: '../medicalRecord/medicalRecord?patientTel=' + patientTel + '&&patientOrderDate=' + patientOrderDate + '&&patientOrderTime=' + patientOrderTime,
    })
  },

  /**
   * 跳转病人信息
   */
  clickPatient: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    var patientTel = JSON.stringify(that.data.patient[index].patientTel)
    var patientOrderDate = JSON.stringify(that.data.patient[index].orderDate)
    var patientName = JSON.stringify(that.data.patient[index].patientName)
    wx.navigateTo({
      url: '../patientMessage/patientMessage?patientTel=' + patientTel + '&&patientOrderDate=' + patientOrderDate + '&&name=' + patientName,
    })
  },

   /**
   * 医生点击取消该患者的预约
   */
  cancelAppointment: function(e) {
    var id = e.currentTarget.dataset.idx
    var tel = this.data.patient[id].patientTel
    var orderDate = this.data.patient[id].orderDate
    var orderTime = this.data.patient[id].orderTime
    var that = this
    wx.showModal({
      title: '确定取消预约？',
      success (res) {
        if (res.confirm) {
          wx.request({
            url: that.data.url + 'doctorCancelAppointment',
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
              doctorTel: getApp().globalData.username,
              patientTel: tel,
              bookDate: orderDate,
              bookTime: orderTime,
            },
            success(res) {
              wx.showToast({
                title: '已成功取消此预约',
                icon: 'success',
              })
              that.getPatient()
            },
            fail(res) {
              wx.showToast({
                title: '预约取消失败',
                icon: 'none',
                duration: 2000
              })
            }
          })
        } 
        else if (res.cancel) {
          wx.showToast({
            title: '未取消该预约',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 医生点击与患者交谈
   */
  chatWithPatient: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    var otherTel = JSON.stringify(that.data.patient[index].patientTel)
    var otherName = JSON.stringify(that.data.patient[index].patientName)
    wx.navigateTo({
        url: '../onlineConsulting/onlineConsulting?otherTel=' + otherTel + '&&otherName=' + otherName,
    })
  }
})